from rest_framework import serializers
from goods.models import SKU

class CartDeleteSerializer(serializers.Serializer):
    sku_id = serializers.IntegerField(label='商品', min_value=1)
    def validate_sku_id(self, value):
        try:
            SKU.objects.get(id=value)
        except SKU.DoesNotExist:
            raise serializers.ValidationError('sku_id不存在')
        return value


class CartSKUSerializer(serializers.ModelSerializer):
    """购物车商品数据序列化器"""
    count = serializers.IntegerField(label='数量')
    selected = serializers.BooleanField(label='是否勾选')

    class Meta:
        model = SKU
        fields = ['id', 'count', 'name', 'default_image_url', 'price', 'selected']

class CartSerializer(serializers.Serializer):
    """购物车序列化器,新增和修改都可以用"""
    sku_id = serializers.IntegerField(label='商品', min_value=1)
    count = serializers.IntegerField(label='购买数据')
    selected = serializers.BooleanField(default=True, label='商品勾选状态')

    def validate_sku_id(self, value):
        try:
            SKU.objects.get(id=value)
        except SKU.DoesNotExist:
            raise serializers.ValidationError('sku_id不存在')
        return value

class CartSelectedAllSerializer(serializers.Serializer):
    """是否全选序列化器"""
    selected = serializers.BooleanField(label='是否勾选')